//
//  NSView+BBViewAdditions.m
//  BBTouch
//
//  Created by ben smith on 8/13/08.
//  This file is part of BBTouch.
//
//  BBTouch is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  BBTouch is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.

//  You should have received a copy of the GNU Lesser General Public License
//  along with BBTouch.  If not, see <http://www.gnu.org/licenses/>.
// 
//  Copyright 2008 Ben Britten Smith ben@benbritten.com .
//

#import "NSView+BBViewAdditions.h"


@implementation NSView(BBViewAdditions)

// this is really to handle a mouse down event in the composite view, but it got stuck here
// because all the subviews need to call it, and it doesnt hurt to have all the other
// nsviews have it.  it should probably be in a custome NSView parent of all the composite views
// but that is for a later refactoring
// for now it is basically a total hack
-(BOOL)isCompositeMouseDown:(NSEvent*)theEvent
{
	if ([[self superview] respondsToSelector:@selector(didHandleMouseDown:)]) 
		return [[self superview] didHandleMouseDown:theEvent];
	return NO;
}

// i use this all over, so i put it in here so i wouldnt be duping code everywhere
// it draws some text in green and then draws a little gray box behind it. (well actually
// it draws the box first, but you get the idea)
-(void)drawText:(NSString*)text centeredAtPoint:(NSPoint)p;
{
	// might be nice to cache this attrs dict somewhere, TODO: what i just said
	NSDictionary *attrsDictionary;
	
	NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
	[style setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]];
	[style setAlignment:NSCenterTextAlignment];

	attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont messageFontOfSize:15],NSFontAttributeName,[NSColor greenColor],NSForegroundColorAttributeName,style,NSParagraphStyleAttributeName,nil];
	
	NSAttributedString * newString = [[NSAttributedString alloc] initWithString:text attributes:attrsDictionary];
	
	NSSize ssize = [newString size];

	// now draw a little rectangle behind it
	[[NSColor colorWithDeviceWhite:0.0 alpha:0.3] set]; 
	NSRect backing = NSMakeRect(p.x - (ssize.width/2.0) - 5, p.y - (ssize.height/2.0) - 2, ssize.width + 10, ssize.height + 4);
	[NSBezierPath fillRect:backing];
	
	// now draw the text on top
	[newString drawAtPoint:NSMakePoint(p.x - (ssize.width/2.0), p.y - (ssize.height/2.0))];

	// clean up
	[newString release];	
	[style release];
	
}


@end
